package com.cibfintech.rpa.jdwzcx.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    // @Bean
    // public PasswordEncoder passwordEncoder()
    // {
    // return new BCryptPasswordEncoder();
    // }

    // @Bean
    // public UserDetailsService userDetailsService()
    // {
    // JdbcUserDetailsManager manager = new JdbcUserDetailsManager();
    // return manager;
    // }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
        auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
        http.formLogin();

        // http.authorizeRequests().antMatchers("/login", "/logout", "/kaptcha",
        // "/oauth/**").permitAll().anyRequest().authenticated();
        // http.formLogin().loginPage("/login").permitAll();
        // http.logout().permitAll();
        http.sessionManagement().maximumSessions(1);
    }

}
